<?php
class Form extends CI_Controller {


    public function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model('Form_model');
        $this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
    }

    public function index() {
        $this->load->view('header');
        $this->load->view('registrera_view');
        $this->load->view('footer');
    }

    public function registrera() {
        $this->form_validation->set_rules('username', 'Användarnamnet', 'trim|required|min_length[5]|max_length[12]|xss_clean|callback_username_check');
        $this->form_validation->set_rules('pass', 'Lösenordet', 'trim|required|matches[passconf]|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('passconf', 'Lösenords bekräftelse', 'trim|required');
        $this->form_validation->set_rules('fname', 'Förnamn', 'trim|required');
        $this->form_validation->set_rules('lname', 'Efternamn', 'trim|required');
        $this->form_validation->set_rules('email', 'E-post', 'trim|required|valid_email');
        $this->form_validation->set_rules('tele', 'Telefon nummret', 'trim|required|numeric');

        $this->form_validation->set_message('required', 'Fältet för %s måste vara ifyllt');
        $this->form_validation->set_message('min_length', '%s måste vara minst 5 tecken långt');
        $this->form_validation->set_message('max_length', '%s får max vara 12 tecken långt');
        $this->form_validation->set_message('matches', 'De två lösenorden matchar inte varandra');
        $this->form_validation->set_message('valid_email', 'Ange en korrekt e-postadress');
        $this->form_validation->set_message('numeric', ' %s får endast bestå av siffror');

        

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('header');
            $this->load->view('registrera_view');
            $this->load->view('footer');
        } else {
            echo "yes";
        }
    }


    public function username_check($username){


            if($this->Form_model->username_check($username)){
                $this->form_validation->set_message('username_check', ' %s finns redan,välj ett annat ');
		return FALSE;
            }else{
                return TRUE;
            }
    }

}

?>
